######################################################################
#Replication file for "The Impact of China's AIIB on the World Bank"
#Authors: Jing Qian, James Vreeland, Jianzhi Zhao
#Codes to replicate Figure D.1
######################################################################
rm(list = ls())

#To install the bpCausal package (ver 0.0.1)
# install.packages('devtools', repos = 'http://cran.us.r-project.org') # if not already installed
# devtools::install_github('liulch/bpCausal')

#Load bpCausal package
library(bpCausal)

#Other packages used
library(tidyverse)
library(RColorBrewer)

#Setwd
setwd("C:/Users/qianj/Dropbox (Princeton)/AIIB_WB_Replication")

#Load custom functions
#Note: Given the number of tests performed in the appendix, 
#several custom functions are created to implement estimation methods in a more efficient way.
#These functions merely create a wrap-up of existing functions, like bpCausal and gsynth,
#in order to avoid typing parameters repeatedly.

source("code/custom_functions.R")

###################################
#Results for Figure D.1
###################################
#----------------
#(1) Load data
#----------------
load("result/bpcausal_original.RData") #Original results
load("data/data_aiib.RData") #AIIB projects

###################################
#Produce Figure D.1
###################################
#------------------
#(1) Prepare results
#------------------
#Groups
iso2c.tr = result.original$raw.id.tr %>%
  unique() #All treated id
iso2c.proj.2019 = df.aiib$iso2c %>%
  unique() %>%
  intersect(iso2c.tr) #AIIB founders with projects by 2019
iso2c.no.proj.2019 = setdiff(iso2c.tr,
                             iso2c.proj.2019) #AIIB founders without projects by 2019
#Summarize
result.use = effSummary(result.original)
result.proj.use = effSummary(result.original,
                             usr.id = iso2c.proj.2019)
result.no.proj.use = effSummary(result.original,
                                usr.id = iso2c.no.proj.2019)

#----------------
#(2) Parameters
#----------------
#Parameters
n = 1
xlim = c(-2, 2)
cex = 2
xtext = 0.6
colors = brewer.pal(n = 3,
                    name = "Dark2")
gap = 1

#-----------------
#(3) Plot
#-----------------
#----------
#Setup
png(filename = "figure/Figure_D1.png",
    height = 800,
    width = 1200)

par(mar = c(5, 1, 5, 1))

#----------
#Empty plot
plot(1,
     type = "n",
     xlab = "Estimated Average Treatment Effect on the Treated",
     ylab = "",
     yaxt = "n",
     xaxt = "n",
     xlim = xlim,
     ylim = c(0, 4),
     cex.lab = 2)
box()


#------------------
#Hard
#------------------
#Original
points(x = result.use$est.avg[1],
       y = 3,
       col = colors[1],
       pch = 19,
       cex = cex)

arrows(x0 = result.use$est.avg[2],
       x1 = result.use$est.avg[3],
       y0 = 3,
       y1 = 3,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[1],
       cex = cex,
       lwd = cex)

#With AIIB Proj 
points(x = result.proj.use$est.avg[1],
       y = 2,
       col = colors[2],
       pch = 19,
       cex = cex)

arrows(x0 = result.proj.use$est.avg[2],
       x1 = result.proj.use$est.avg[3],
       y0 = 2,
       y1 = 2,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[2],
       cex = cex,
       lwd = cex)

#Without AIIB Proj 
points(x = result.no.proj.use$est.avg[1],
       y = 1,
       col = colors[3],
       pch = 19,
       cex = cex)

arrows(x0 = result.no.proj.use$est.avg[2],
       x1 = result.no.proj.use$est.avg[3],
       y0 = 1,
       y1 = 1,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[3],
       cex = cex,
       lwd = cex)

#------------------
#Other elements
#------------------
#Abline
abline(v = 0,
       lty = "dashed",
       lwd = cex,
       col = "gray")
#Legend
legend("topright",
       legend = c("All AIIB Founders",
                  "Participated in AIIB Projects by 2019",
                  "No AIIB Projects by 2019"),
       cex = cex,
       col = colors,
       lty = c(1, 1),
       bty = "n",
       lwd = cex)

#Axis
axis(side = 1,
     at = seq(min(xlim),
              max(xlim),
              1),
     cex.axis = cex)

#Title
title("World Bank Infrastructure Projects",
      cex.main = cex,
      line = 1)
#----------
#Close
dev.off()